﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        Image a;
        int x; int y;
        //bool b_mouseenter = false;
        public Point Picker1 = new Point(-1, -1);
        public Point Picker_l = new Point(-1, -1);
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.Cursor = Cursors.Hand;
            Picker1.X = MousePosition.X;
            Picker1.Y = MousePosition.Y;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pictureBox1.Cursor = Cursors.Arrow;
            Picker_l.X = MousePosition.X;
            Picker_l.Y = MousePosition.Y;
            x = Picker_l.X - Picker1.X;
            y = Picker_l.Y - Picker1.Y;
            if (pictureBox1.Height != panel1.Height || pictureBox1.Width != panel1.Width)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X + x, pictureBox1.Location.Y + y);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(Application.StartupPath + "\\Auto.txt");
            while (!sr.EndOfStream)
            {
               string s= sr.ReadLine();
               listBox1.Items.Add(s);
            }
            sr.Close();
            a = Image.FromFile("map.jpg");
            pictureBox1.Image = a;
            bmpheight = a.Height;
            bmpwidth  = a.Width;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromImage(a);
            Pen p=new Pen(Color.Blue);
            g.DrawEllipse(p, bmpx, bmpy, 20, 20);
            pictureBox1.Image = a;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image.Save("map1.jpg");
            
        }
        int bmpwidth, bmpheight;
        int bmpx, bmpy;
        public Point Picker = new Point(-1, -1);
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            //Color c;
            Picker.X = e.X;
            Picker.Y = e.Y;
            Point m = pictureBox1.PointToScreen(new Point(Picker.X, Picker.Y));
            //c = PickColor(m.X, m.Y);
            double ratex = Picker.X * 1.00 / pictureBox1.Width;
            double ratey = Picker.Y * 1.00 / pictureBox1.Height;
             bmpx = (int)(bmpwidth * ratex);
             bmpy = (int)(bmpheight * ratey);
            txtPixel.Text = "[" + bmpx + "," + bmpy + "] ";
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                foreach (var v in listBox1.Items)
                { 
                    StartwithWindows(@"" + v.ToString(), true);
                }
                //StartwithWindows(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe", true);
            }
            else
            {
                foreach (var v in listBox1.Items)
                {
                    StartwithWindows(@"" + v.ToString(), false);
                }
                //StartwithWindows(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe", false);
            }
        }
        private void StartwithWindows(string filename, bool IsAutoRun)
        {
            RegistryKey reg = null;
            try
            {
                if (!System.IO.File.Exists(filename))
                {
                    throw new Exception("文件不存在！");
                }
                string name = filename.Substring(filename.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (IsAutoRun)
                {
                    reg.SetValue(name, filename);
                }
                else
                {
                    reg.SetValue(name, false);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if (reg != null)
                {
                    reg.Close();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Model files(*.exe)|*.exe";
            string s;
            if (DialogResult.OK == of.ShowDialog())
            {
                s = of.FileName;
                foreach (var v in listBox1.Items)
                {
                    if (v.ToString() == s)
                    {
                        MessageBox.Show("已添加了该项！");
                        return;
                    }
                }
                listBox1.Items.Add(s);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(Application.StartupPath+"\\Auto.txt");
                foreach (var v in listBox1.Items)
                {
                    sw.WriteLine(v.ToString());
                }
                sw.Flush();
                sw.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int n = listBox1.SelectedIndex;
            if (n >=0)
            {
                listBox1.Items.RemoveAt(n);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(Application.StartupPath + "\\Auto.txt");
                foreach (var v in listBox1.Items)
                {
                    sw.WriteLine(v.ToString());
                }
                sw.Flush();
                sw.Close();
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            Cursor.Current = new System.Windows.Forms.Cursor("Cursor1.cur");
            pictureBox1.Cursor = Cursor.Current;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Cursor = Cursors.Arrow;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Model files(*.dll)|*.dll";
            string s;
            if (DialogResult.OK == of.ShowDialog())
            {
                s = of.FileName;
                Process p = new Process();
                p.StartInfo.FileName = "Regsvr32.exe";
                p.StartInfo.Arguments = "/s " + @"" + s;
                p.Start();
                p.Close();
            }
        }
    }
}
